import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { BehaviorSubject, Observable, of } from 'rxjs';
import { catchError, debounceTime, map, switchMap } from 'rxjs/operators';
import { SysroleUrlPermission } from 'src/app/domain/sparrow';
import { SysroleService } from 'src/app/service/sysrole.service';
import { UrlService } from 'src/app/service/url.service';

export interface AutocompleteOptionGroups {
  title: string;
  count?: number;
  children?: AutocompleteOptionGroups[];
}


@Component({
  selector: 'app-url-permission',
  templateUrl: './url-permission.component.html',
  styleUrls: ['./url-permission.component.css']
})
export class UrlPermissionComponent implements OnInit {
  randomUserUrl = 'https://api.randomuser.me/?results=5';
  searchChange$ = new BehaviorSubject('');
  optionList: string[] = [];
  selectedUser?: string;
  isLoading = false;
  currentPermissions: SysroleUrlPermission[] = [];
  // selectedUrls :

  onSearch(value: string): void {
    this.isLoading = true;
    this.searchChange$.next(value);
  }

  onClose() {

  }

  constructor(private http: HttpClient, private sysroleService: SysroleService, private urlService: UrlService, private router: ActivatedRoute) { }

  ngOnInit(): void {
    /* eslint-disable @typescript-eslint/no-explicit-any */
    const getRandomNameList = (name: string) =>
      this.sysroleService.list({page: 0, size: 10})
        .pipe(
          catchError(() => of({ results: [] })),
          map((res: any) => res.content)
        )
        .pipe(map((list: any) => list.map((item: any) => `${item.name} ${name}`)));
    const optionList$: Observable<string[]> = this.searchChange$
      .asObservable()
      .pipe(debounceTime(500))
      .pipe(switchMap(getRandomNameList));
    optionList$.subscribe(data => {
      this.optionList = data;
      this.isLoading = false;
    });


    let ids: string | any = this.router.snapshot.paramMap.get("ids");
    this.urlService.getPermissionById(JSON.parse(ids)).subscribe(res => {
      this.currentPermissions = res.content
    })
  }

}
// function catchError(arg0: () => Observable<{ results: never[]; }>): import("rxjs").OperatorFunction<Object, unknown> {
//   throw new Error('Function not implemented.');
// }

// function debounceTime(arg0: number): import("rxjs").OperatorFunction<string, unknown> {
//   throw new Error('Function not implemented.');
// }

// function switchMap(getRandomNameList: (name: string) => Observable<unknown>): import("rxjs").OperatorFunction<unknown, string[]> {
//   throw new Error('Function not implemented.');
// }

